CXX_COMMON_FLAGS+="-m64 -g -pipe -std=c++2b -stdlib=libc++ -pthread "
CXX_COMMON_FLAGS+="-ferror-limit=10 -I${PROJECT_DIR} -I${REPO_ROOT_DIR} "
CXX_COMMON_FLAGS+="-D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64 "
CXX_COMMON_FLAGS+="-msse4 -mavx2 -mlzcnt -mpclmul -mbmi "

LIB_BASE64_OBJ+="${REPO_ROOT_DIR}/stx/ext/base64/lib/libbase64.o"
PCH_FILE_NAME="pc.hpp"
EXE_NAME="x"

CXX_LINTER_FLAGS="-Wall -Wextra -Werror "
CXX_LINTER_FLAGS+="-Wconversion "
CXX_LINTER_FLAGS+="-Wold-style-cast "
CXX_LINTER_FLAGS+="-Woverloaded-virtual "
CXX_LINTER_FLAGS+="-Wpointer-arith "
CXX_LINTER_FLAGS+="-Wshadow "
CXX_LINTER_FLAGS+="-Wwrite-strings "

CXX_RELEASE_FLAGS+="-DNDEBUG -Ofast -fuse-ld=gold -flto "

#ASAN_FLAGS="-O1 -fsanitize=address -fno-omit-frame-pointer "
ASAN_FLAGS=""

LINK_FLAGS+="-L/usr/local/lib "
LINK_FLAGS+="-Wl,-rpath=/usr/local/lib "
LINK_FLAGS+="-Wl,-rpath-link=/usr/local/lib "
LINK_FLAGS+="-lcrypto -lssl -lanl -latomic -ldl -lrt -lz "
LINK_FLAGS+="-lc++abi -lc -lm -lc++ -lgcc_s -lpthread -lc_nonshared "

if [[ "init" == "$1" ]]; then
    echo "make init"

    if [[ ${REPO_ROOT_DIR} != $(pwd) ]];
	then
	    rm -f ${PROJECT_DIR}/.clang-format
		rm -f ${PROJECT_DIR}/.clang-tidy
		rm -f ${PROJECT_DIR}/clang-tidy.sh
		rm -f ${PROJECT_DIR}/.clangd
		rm -f ${PROJECT_DIR}/common_mk.sh

		ln -s ${REPO_ROOT_DIR}/.clang-format ${PROJECT_DIR}/.clang-format
		ln -s ${REPO_ROOT_DIR}/.clang-tidy ${PROJECT_DIR}/.clang-tidy
		ln -s ${REPO_ROOT_DIR}/clang-tidy.sh ${PROJECT_DIR}/clang-tidy.sh
		ln -s ${REPO_ROOT_DIR}/.clangd ${PROJECT_DIR}/.clangd
		ln -s ${REPO_ROOT_DIR}/common_mk.sh ${PROJECT_DIR}/common_mk.sh
	fi

	echo ""
	exit
fi

if [[ "tidy" == "$1" ]]; then
    echo "make tidy"
    ${PROJECT_DIR}/clang-tidy.sh	
	echo ""
	exit
fi

function format()
{
	echo "clang-format: $1"
    find $1 -maxdepth 1 -iname "*.hpp" -o -iname "*.cxx" -o -iname "*.cpp" \
	-o -iname "*.proto" | xargs -I{} clang-format -i {} --style=file
}

if [[ "format" == "$1" ]]; then
    echo "make format"
    format ${REPO_ROOT_DIR}/stx/core
	format ${REPO_ROOT_DIR}/stx/cxx
	format ${REPO_ROOT_DIR}/stx/3rd
	format ${PROJECT_DIR}
	echo ""
	exit
fi

if [[ "release" == "$1" ]]; then
    echo "make release"
	cd ${REPO_ROOT_DIR}/stx/ext/base64
	./mk.sh
	cd ${PROJECT_DIR}
    time clang++ ${CXX_COMMON_FLAGS} ${CXX_LINTER_FLAGS} ${CXX_RELEASE_FLAGS} \
	    ${LIB_BASE64_OBJ} stable.cpp main.cpp \
		-o ${RELEASE_TARGET_NAME} ${LINK_FLAGS}
	echo ""
	exit
fi

if [[ "test_release" == "$1" ]]; then
    echo "make test_release"
	cd ${PROJECT_DIR}
    time clang++ ${CXX_COMMON_FLAGS} ${CXX_LINTER_FLAGS} ${CXX_RELEASE_FLAGS} \
	    ${LIB_BASE64_OBJ} stable.cpp test.cpp -o ${EXE_NAME} ${LINK_FLAGS}
	echo ""
	exit
fi

if [[ "pch" == "$1" ]]; then
    echo "make pch"
	cd ${PROJECT_DIR}
    time clang++ ${CXX_COMMON_FLAGS} ${CXX_LINTER_FLAGS} ${ASAN_FLAGS} \
	-fno-lto -x c++-header ${PCH_FILE_NAME} -o ${PCH_FILE_NAME}.pch
	echo ""
	exit
fi

if [[ "stable" == "$1" ]]; then
	echo "make stable"
	cd ${PROJECT_DIR}
    time clang++ ${CXX_COMMON_FLAGS} ${CXX_LINTER_FLAGS} ${ASAN_FLAGS} \
		-fno-lto -include ${PCH_FILE_NAME} -c stable.cpp -o stable.o
	echo ""
	exit
fi

if [[ "dbg" == "$1" ]]; then
	echo "make dbg"
	cd ${PROJECT_DIR}
    time clang++ ${CXX_COMMON_FLAGS} ${CXX_LINTER_FLAGS} ${ASAN_FLAGS} \
		-fno-lto -include ${PCH_FILE_NAME} ${LIB_BASE64_OBJ} main.cpp stable.o \
		-o ${EXE_NAME} ${LINK_FLAGS} 
	echo ""
	exit
fi

if [[ "test" == "$1" ]]; then
	echo "make test"
	cd ${PROJECT_DIR}
    time clang++ ${CXX_COMMON_FLAGS} ${CXX_LINTER_FLAGS} ${ASAN_FLAGS} \
		-fno-lto -include ${PCH_FILE_NAME} ${LIB_BASE64_OBJ} test.cpp stable.o \
		-o ${EXE_NAME} ${LINK_FLAGS}
	echo ""
	exit
fi

if [[ "x3" == "$1" ]]; then
    cd ${PROJECT_DIR}	
	${PROJECT_DIR}/mk.sh pch && \
	${PROJECT_DIR}/mk.sh stable && \
	${PROJECT_DIR}/mk.sh dbg
	exit
fi

if [[ "t3" == "$1" ]]; then
    cd ${PROJECT_DIR}	
	${PROJECT_DIR}/mk.sh pch && \
	${PROJECT_DIR}/mk.sh stable && \
	${PROJECT_DIR}/mk.sh test
	exit
fi

if [[ "x4" == "$1" ]]; then
    cd ${PROJECT_DIR}
	${PROJECT_DIR}/mk.sh x3 && ${PROJECT_DIR}/x
	exit
fi

if [[ "t4" == "$1" ]]; then
    cd ${PROJECT_DIR}
	${PROJECT_DIR}/mk.sh t3 && ${PROJECT_DIR}/x
	exit
fi